package com.nondo.dean.order.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.order.domain.OrderLog;
import com.nondo.dean.order.domain.OrderLogExample;
import com.nondo.dean.order.mapper.OrderLogMapper;

/**
 * 订单日志管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月19日
 */
@Component
public class OrderLogService {

    @Autowired
    private OrderLogMapper orderLogMapper;

    /**
     * 插入订单日志
     * @param orderLog
     * @return
     */
    @Transactional
    public int addOrderLog(OrderLog orderLog) {
        return orderLogMapper.insertSelective(orderLog);
    }

    /**
     * 修改订单日志
     * @param orderLog
     * @return
     */
    @Transactional
    public int updateOrderLog(OrderLog orderLog) {
        return orderLogMapper.updateByPrimaryKeySelective(orderLog);
    }

    /**
     * 按照主键ID查询订单日志
     * @param id
     * @return
     */
    public OrderLog selectOrderLog(Integer id) {
        return orderLogMapper.selectByPrimaryKey(id);
    }

    /**
     * 订单日志分页查询列表
     * @param offset
     * @param limit
     * @param orderLog
     * @return
     */
    public List<OrderLog> getOrderLogList(int offset, int limit, OrderLog orderLog) {
        OrderLogExample example = new OrderLogExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        OrderLogExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, orderLog);
        return orderLogMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 订单日志总数统计
     * @param orderLog
     * @return
     */
    public Long count(OrderLog orderLog) {
        OrderLogExample example = new OrderLogExample();
        OrderLogExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, orderLog);
        return orderLogMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param orderLog
     */
    void setCriteria(OrderLogExample.Criteria criteria, OrderLog orderLog) {
        if (orderLog != null) {
            if (orderLog.getId() != null)
                criteria.andIdEqualTo(orderLog.getId());
            if (StringUtils.isNotBlank(orderLog.getOrderCode()))
                criteria.andOrderCodeLike("%" + orderLog.getOrderCode() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

    /**
     * 按照订单号查询订单日志列表
     * @return 订单日志列表
     */
    public List<OrderLog> getOrderLogList(String orderCode) {
        OrderLogExample example = new OrderLogExample();
        example.setOrderByClause("create_date DESC");
        OrderLogExample.Criteria criteria = example.createCriteria();
        criteria.andOrderCodeLike("%" + orderCode + "%");
        criteria.andDelFlagEqualTo(String.valueOf(0));
        return orderLogMapper.selectByExample(example);
    }
}
